#pragma once

#ifndef HTTP_H_
#define HTTP_H_

#include <stdio.h>
#include "../common/list.h"
#include "../common/memory.h"
#include "http_req_elements.h"
#include "http_resp_elements.h"
#include "http_model.h"

#define	find_header(ptr, hs, type)	ptr = hs[type]
#define get_header(hs, type)		hs[type]
#define set_data(d,s,l)		free_s(d); d=malloc(l); memcpy(d,s,l)

#define HTTP_STATUS_CONNECTED	0x01
#define HTTP_STATUS_DISCONNECTED	0x02
#define HTTP_STATUS_NOCHANGE	0x00

#define HTTP_HEADER_LEN		20
#define HTTP_MAX_HEADERS	20
#define HTTP_CRLF_TYPE	0
#define HTTP_VERSION_TYPE 0
#define HTTP_DATA_BUFFER_LEN 8192

static int http_CRLF_len = 1;
static char *http_CRLF[] = {"\r\n"};

static int http_version_len = 1;
static char *http_version[] =
{
	"HTTP/1.1"
};

static char *http_header_delimiter[] =
{
	":"
};

//pacchetto http di tipo request
typedef struct {
	int	method;
	int	uri;
	int	version;
	char	headers[HTTP_MAX_HEADERS][HTTP_HEADER_LEN];
	//list	*headers;
	char	data[HTTP_DATA_BUFFER_LEN];
	size_t	data_len;
} http_req;

//pacchetto http di tipo response
typedef struct {
	int	version;
	int	code;
	int	reason;
	char 	headers[HTTP_MAX_HEADERS][HTTP_HEADER_LEN];
	char	data[HTTP_DATA_BUFFER_LEN];
	size_t	data_len;
} http_resp;

enum{
	HTTP_MSG_TYPE_RQST = 0x00,
 HTTP_MSG_TYPE_RSPN
};

//contiene un messaggio di richiesta ed uno di risposta
typedef struct {
	http_req	req;
	http_resp	resp;
} http_messages;

void clean_all_headers(http_messages*);
int find_content_len(char[HTTP_MAX_HEADERS][HTTP_HEADER_LEN], int);
void set_header(char[HTTP_MAX_HEADERS][HTTP_HEADER_LEN], int , char *);
void set_header_len(char[HTTP_MAX_HEADERS][HTTP_HEADER_LEN], int , char *, int);
void set_header_int(char[HTTP_MAX_HEADERS][HTTP_HEADER_LEN], int , int);
void printf_request(http_req *);
void printf_response(http_resp *);


#endif
